package com.zlsf;


import com.zlsf.conf.Config;
import com.zlsf.verticles.MediaVerticle;
import com.zlsf.verticles.WebVerticle;
import io.vertx.core.*;
import io.vertx.core.spi.cluster.ClusterManager;
import io.vertx.spi.cluster.zookeeper.ZookeeperClusterManager;
import lombok.extern.slf4j.Slf4j;


@Slf4j
public class MyLauncher extends Launcher {

  public MyLauncher() {
  }


  public static void main(String[] args) {
    MyLauncher:
    launchCluster();

  }

  /**
   * 加入集群
   */
  private static void launchCluster() {
    log.info("加入集群... ...");
    ClusterManager mgr = new ZookeeperClusterManager();
    VertxOptions options = new VertxOptions().setClusterManager(mgr);
    Vertx.clusteredVertx(options, res -> {
      log.info("加入集群 success");
      if (res.succeeded()) {
        Config.getInstance().loadConfig(res.result(), ar -> {
          regestVerticles(res);
        });
      } else {
        log.error("加入集群 error", res.cause());
      }
    });
  }


  /**
   * 注册Verticles
   *
   * @param res
   */
  private static void regestVerticles(AsyncResult<Vertx> res) {
    Vertx vertx = res.result();
    DeploymentOptions options = new DeploymentOptions().setInstances(4);
    //媒体的
    vertx.deployVerticle(MediaVerticle.class.getName(), options);

    //集群解码的
    //vertx.deployVerticle(ConvertDataVerticle.class.getName(), options);
    //vertx.deployVerticle(TestVerticles.class.getName());
    //web的
    vertx.deployVerticle(WebVerticle.class.getName());
  }


}
